
#region

using System.Collections.Generic;
using UserStoryTrackerDemo.Domain.Core.Pocos;
using UserStoryTrackerDemo.Domain.Pocos;

#endregion

namespace UserStoryTrackerDemo.Data
{
	public class Database
	{
		private static Database _db;
		private readonly List<UserStory> _userStories;

		private Database()
		{
			_userStories = new List<UserStory>()
			               	{
			               		new UserStory()
			               			{
			               				AsA = "user of the system",
			               				ICan = "see user stories",
			               				SoThatBecause = "I know what to work on",
			               				Id = 1
			               			},
			               		new UserStory()
			               			{
			               				Id = 2,
			               				AsA = "Developer",
			               				ICan = "test this page",
			               				SoThatBecause = "I can see it work",
			               				Completed = false,
			               			},
			               		new UserStory()
			               			{
			               				Id = 3,
			               				AsA = "Developer",
			               				ICan = "test this repository",
			               				SoThatBecause = "I can see it return data",
			               				Completed = false,
			               			},
			               	};
		}

		public static Database Instance
		{
			get { return _db ?? (_db = new Database()); }
		}

		public List<UserStory> GetUserStoryData()
		{
			return _userStories;
		}

		public void SavePoco(IPoco iPoco)
		{
			if (iPoco.GetType() == typeof (UserStory))
				_userStories.Add((UserStory) iPoco);
		}

		public void UpdatePoco(IPoco iPoco)
		{
			if (iPoco.GetType() == typeof (UserStory))
			{
				var result = _userStories.Find(us => us.Id == iPoco.Id);
				if (result != null)
				{
					var newUserStory = (UserStory) iPoco;
					result.AsA = newUserStory.AsA;
					result.Completed = newUserStory.Completed;
					result.ICan = newUserStory.ICan;
					result.SoThatBecause = newUserStory.SoThatBecause;
				}
			}
		}
	}
}